Repository navigation
Conversation
The association scopes ordered by the hierarchy table's generations column, but the preloader loads the source records in a query that doesn't join that table, so preload/includes raised ActiveRecord::StatementInvalid (PostgreSQL: missing FROM-clause entry; SQLite: no such column). The through associations already order by generations. Rails applies that order when loading lazily and keeps the through-record order when preloading, so the extra order is dropped from self_and_ancestors, and from self_and_descendants when no order option is set. Lazy SQL is unchanged. Refs ClosureTree#137, ClosureTree#70
yskttm
marked this pull request as ready for review
December 2, 2026 06:34
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #137
What this fixes
Preloading
self_and_ancestorsfails on every adapter:self_and_descendantsfails the same way on models without anorder:option.Why
Both associations order by
<hierarchy>.generations. A lazy load joins the hierarchy table, so the order works. A preload loads the through records first and then runsSELECT tags.* WHERE id IN (...), which doesn't join the hierarchy table, so the order is invalid there.The fix
Drop the order from
self_and_ancestors, and fromself_and_descendantswhen there's noorder:option.The through associations (
ancestor_hierarchiesanddescendant_hierarchies) already order bygenerations. Rails applies that order when it loads lazily, and keeps the through records' order when it preloads, so the results are still ordered by generations.self_and_descendantswith anorder:option keeps its order. It needs generations first and then the order column, which the through association's order can't express. That case still can't be preloaded.Testing
test/closure_tree/preload_test.rb:preloadandincludesofself_and_ancestorson the PostgreSQL, MySQL and SQLite tag models, plusself_and_descendantson the adoptable tag models. Checks the order andancestry_path. All 18 tests error on master and pass with the fix.